import json
import os

from models import *

from flask import Flask, request


data_dir = os.path.abspath('./data/')
app = Flask(__name__)


@app.route('/algorithm', methods=['POST', 'GET'])
def algorithm():
    args = json.loads(request.get_data().decode("utf8"))
    try:
        model = args['model'].upper()
        dataset = os.path.join(data_dir, args['dataset'])
        data = eval(model)(dataset, args).run()
        return {
            "status": 200,
            "msg": "成功",
            "data": data
        }
    except Exception as exc:
        return {
            "status": 500,
            "msg": str(exc)
        }


@app.route('/statistic', methods=['POST', 'GET'])
def statistic():
    args = json.loads(request.get_data().decode("utf8"))
    try:
        dataset = os.path.join(data_dir, args['dataset'])
        data = Statistic(dataset, args).run()
        return {
            "status": 200,
            "msg": "成功",
            "data": data
        }
    except Exception as exc:
        return {
            "status": 500,
            "msg": str(exc)
        }


if __name__ == '__main__':
    app.run(debug=True)
